package com.tangyuan.user.controller;

import com.tangyuan.common.result.Result;
import com.tangyuan.dto.UserSetPhoneNumberDto;
import com.tangyuan.model.User;
import com.tangyuan.common.util.CommStatus;
import com.tangyuan.user.service.IUserService;
import com.tangyuan.user.service.UserBlackListService;
import com.tangyuan.user.service.UserConcernService;
import com.tangyuan.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user/setting")
public class UserInfoController {
    @Autowired
    private UserInfoService userService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserConcernService userConcernService;
    @Autowired
    private UserBlackListService userBlackListService;

    //用户进入设置界面获取基本数据
    @GetMapping("/info/getSetingInfo")
    public Result getSettingInfo(@RequestHeader(name = "userId",required = false) Long userId){
      return userService.getSettingInfo(userId);
    }

    //用户修改头像
    @PostMapping("/info/uploadHead")
    public Result handleFileUpload(@RequestParam(name = "file",required = false) MultipartFile file,
                                   @RequestHeader(name = "Authorization",required = false) String token,
                                   @RequestParam(name = "url",required = false) String url) {
        return userService.handleFileUpload(file,token,url);
    }

    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile file,
                                   @RequestHeader(name = "Authorization",required = false) String token) {
        return userService.fileUpload(file);
        
    }

    //用户修改密码
    @GetMapping("/info/resetPassword")
    public Result resetPassword(@RequestHeader(name = "Authorization",required = false) String token,
                                @RequestParam("prePasswd") String prePasswd,
                                @RequestParam("newPasswd") String newPasswd) {
        return userService.resetPassword(prePasswd,newPasswd,token);
    }


    //昵称设置
    @GetMapping("/info/setNickName")
    public Result setNickName(@RequestHeader(name = "Authorization",required = false) String token,@RequestParam("nickName") String nickName ){
        return userService.setNickName(nickName,token);
    }
    //手机号重新设置,原手机号设置发送验证码
    @GetMapping("/info/setPhoneNumber/getcode")
    public Result setPhoneNumberGetCode(@RequestParam("mobile") String mobile){
        return userService.setPhoneNumberGetCode(mobile);
    }

    //原手机号验证验证码
    @PostMapping("/info/setPhoneNumber")
    public Result setPhoneNumber(@RequestBody UserSetPhoneNumberDto userSetPhoneNumberDto ){
        return userService.setPhoneNumber(userSetPhoneNumberDto);
    }

    //新手机号发送验证码
    @GetMapping("/info/setNewPhoneNumber/getcode")
    public Result setNewPhoneNumberGetCode(@RequestParam("mobile") String mobile){
        return userService.setNewPhoneNumberGetCode(mobile);
    }

    //新手机号验证验证码并重新设置密码
    @PostMapping("/info/setNewPhoneNumber")
    public Result setNewPhoneNumber(@RequestHeader(name = "Authorization",required = false) String token,@RequestBody UserSetPhoneNumberDto userSetPhoneNumberDto ){
        return userService.setNewPhoneNumber(userSetPhoneNumberDto,token);
    }
    //修改生日
    @GetMapping("/info/setUserBirthday")
    public Result setUserBirthday(@RequestHeader(name = "Authorization",required = false) String token,@RequestParam String Birthday){
        iUserService.setUserById(User.BIRTH_DAY,Birthday,token);
        return Result.ok();
        
    }


    //修改性别
    @GetMapping("/info/setUserSex")
    public Result setUserSex(@RequestHeader(name = "Authorization",required = false) String token,Integer sex){
        iUserService.setUserById(User.SEX,sex,token);
        return Result.ok();
    }
    //展示当前用户的黑名单
    @GetMapping("/info/showBlackList")
    public Result showBlackList(@RequestHeader(name = "Authorization",required = false) String token){
        return userService.showBlackList(token);
    }
    //添加或移除当前用户选择用户黑名单
    @GetMapping("/info/addremoveBlackList")
    public Result removeBlackList(@RequestHeader(name = "Authorization",required = false)  String token,@RequestParam Long id){
        return userBlackListService.removeBlackList(CommStatus.getUser(token),id);
    }

    //当前用户关注其他用户,取消该用户关注
    @GetMapping("/info/addConcern")
    public Result addConcern(@RequestHeader(name = "Authorization",required = false) String token,
                             @RequestParam Long concerUserId,
                             @RequestParam Integer type){
        User user = CommStatus.getUser(token);
        return userConcernService.addConcern(user,concerUserId,type);
        
    }
    //查看当前软件版本
    @GetMapping("/info/checkVersion")
    public Result checkVersion(){
       return userService.checkVersion();
    }

    //检查当前软件版本是否更新
    @GetMapping("/info/checkForceUpdate")
    public Result checkForceUpdate(@RequestParam String currentVersion,@RequestParam String deviceInfo){
        return userService.checkForceUpdate(currentVersion,deviceInfo);
    }

    //用户已读须知
    @GetMapping("/info/readNotice")
    public Result readNotice(@RequestHeader(name = "Authorization", required = false) String token) {
        iUserService.setUserById(User.IS_READ_NOTICE,1,token);
        return Result.ok();
    }

    // 用户注销 2024/5/18 13:01 By 少帅
    @GetMapping("/info/deleteUser")
    public Result deleteUser(@RequestHeader(name = "Authorization") String token) {
        return userService.deleteUser(token);
    }


}
